define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'user.help_user/index',
        add_url: 'user.help_user/add',
        edit_url: 'user.help_user/edit',
        delete_url: 'user.help_user/delete',
        export_url: 'user.help_user/export',
        modify_url: 'user.help_user/modify',
        recycle_url: 'user.help_user/recycle',
        info_url: 'user.help_user/info',
    };

    return {

        index: function () {
            ea.table.render({
                init: init,
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', search: false, title: 'id'},
                    {field: 'user_type', search: false, selectList: typeSelects, title: '用户类型'},
                    {field: 'openid', search: false, title: '微信openid'},
                    {field: 'nickname', title: '昵称'},
                    {field: 'mobile', title: '手机号'},
                    {field: 'avatar', search: false, title: '头像', templet: ea.table.image},
                    {field: 'gender', search: 'select', selectList: sexSelects, title: '性别'},
                    {
                        field: 'helpInfo.approve_status',
                        search: 'select',
                        selectList: approveStatusSelects,
                        title: '审批状态',
                        templet: function (d) {
                            // 添加安全检查
                            if (!d.helpInfo || d.helpInfo.approve_status === undefined || d.helpInfo.approve_status === null) {
                                return '<span class="layui-badge layui-bg-gray">未提交信息</span>';
                            }
                            switch (d.helpInfo.approve_status) {
                                case 1:
                                    return '<span class="layui-badge layui-bg-orange">待审批</span>';
                                case 2:
                                    return '<span class="layui-badge layui-bg-blue">审批通过</span>';
                                case 3:
                                    return '<span class="layui-badge layui-bg-red">审批拒绝</span>';
                                default:
                                    return '<span class="layui-badge layui-bg-gray">未提交信息</span>';
                            }
                        }
                    },
                    {
                        field: 'status',
                        search: 'select',
                        selectList: statusSelects,
                        title: '状态',
                        templet: ea.table.switch
                    },
                    {field: 'create_time', search: 'range', title: '创建时间'},
                    {
                        width: 250,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            [
                                {
                                    text: '详情',
                                    url: init.info_url,
                                    method: 'open',
                                    auth: 'info',
                                    class: 'layui-btn layui-btn-xs layui-btn-normal',
                                },
                                {
                                    text: '审批',
                                    url: 'user.help_user/approve',
                                    method: 'open',
                                    auth: 'approve',
                                    class: 'layui-btn layui-btn-xs layui-btn-warm',
                                    condition: function (d) {
                                        // 只有当审批状态为待审批(1)时才显示审批按钮
                                        return d.helpInfo &&
                                            typeof d.helpInfo.approve_status !== 'undefined' &&
                                            d.helpInfo.approve_status !== null &&
                                            d.helpInfo.approve_status == 1;
                                    }
                                }
                            ]
                        ]
                    },

                ]],
            });

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        approve: function () {
            ea.listen();
        },
        recycle: function () {
            init.index_url = init.recycle_url;
            ea.table.render({
                init: init,
                toolbar: ['refresh',
                    [{
                        class: 'layui-btn layui-btn-sm',
                        method: 'get',
                        field: 'id',
                        icon: 'fa fa-refresh',
                        text: '全部恢复',
                        title: '确定恢复？',
                        auth: 'recycle',
                        url: init.recycle_url + '?type=restore',
                        checkbox: true
                    }, {
                        class: 'layui-btn layui-btn-danger layui-btn-sm',
                        method: 'get',
                        field: 'id',
                        icon: 'fa fa-delete',
                        text: '彻底删除',
                        title: '确定彻底删除？',
                        auth: 'recycle',
                        url: init.recycle_url + '?type=delete',
                        checkbox: true
                    }], 'export',
                ],
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', search: false, title: 'id'},
                    {field: 'user_type', search: false, selectList: typeSelects, title: '用户类型'},
                    {field: 'openid', search: false, title: '微信openid'},
                    {field: 'nickname', title: '昵称'},
                    {field: 'avatar', search: false, title: '头像', templet: ea.table.image},
                    {field: 'gender', search: 'select', selectList: sexSelects, title: '性别'},
                    {field: 'mobile', title: '手机号'},
                    {field: 'status', search: 'select', selectList: statusSelects, title: '状态'},
                    {field: 'create_time', search: 'range', title: '创建时间'},
                    {
                        width: 250,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            [{
                                title: '确认恢复？',
                                text: '恢复数据',
                                filed: 'id',
                                url: init.recycle_url + '?type=restore',
                                method: 'get',
                                auth: 'recycle',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                            }, {
                                title: '想好了吗？',
                                text: '彻底删除',
                                filed: 'id',
                                method: 'get',
                                url: init.recycle_url + '?type=delete',
                                auth: 'recycle',
                                class: 'layui-btn layui-btn-xs layui-btn-normal layui-bg-red',
                            }]]
                    }
                ]],
            });

            ea.listen();
        },
    };
});